﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyPhongVePhim.Classes;

namespace QuanLyPhongVePhim.Forms
{
    public partial class frmNhanVien : Form
    {
        #region public varibles
        private int _EmployeeID;

        private DateTime? myselectedDate = null;
        public int EmployeeID
        {
            get
            {
                return _EmployeeID;
            }
            set
            {
                _EmployeeID = value;
            }
        }
        #endregion

        #region events

        public frmNhanVien()
        {
            InitializeComponent();
        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            if (EmployeeID > 0)
            {
                NhanVien nv = new NhanVien();
                nv.MaNhanVien = EmployeeID;
                nv.GetEmppoyeeInfo();
                btnAddNhanVien.Text = Properties.Resources.EMP_UPDATE_BUTTON_TEXT;

                MaptoUI(nv);
            }
            else
            {
                btnAddNhanVien.Text = Properties.Resources.EMP_ADD_BUTTON_TEXT;
            }
        }
        
        private void btnAddNhanVien_Click(object sender, EventArgs e)
        {
            this.btnAddNhanVien.Enabled = false;
            NhanVien nv = new NhanVien();

            nv = MaptoObject();
           
            bool result = nv.UpdateEmployeeInfomation();

            if (result)
            {
                if (EmployeeID > 0)
                {
                    DialogResult box =
                    MessageBox.Show(string.Format(Properties.Resources.EMP_UPDATE_SUCCESS_MES, txtHo.Text.Trim(),txtTen.Text.Trim()));
                  ClosingForm();
                }
                else
                {
                    DialogResult box =
                    MessageBox.Show(string.Format(Properties.Resources.EMP_UPDATE_SUCCESS_MES, txtHo.Text.Trim(),txtTen.Text.Trim()));
                    ClosingForm();
                }
                
            }
            else
            {
                DialogResult box = MessageBox.Show(Properties.Resources.COMMON_UPDATE_UNSUCCESS);

            }

        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == 8) || (e.KeyChar == 46))) //8: Backspace, 46:  delete keys
                e.Handled = true;     
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == 8) || (e.KeyChar==46))) //8: Backspace, 46:  delete keys
                e.Handled = true;     
        }

        private void dtNgayVaoLam_ValueChanged(object sender, EventArgs e)
        {
            myselectedDate = dtNgayVaoLam.Value;
        }

        #endregion

        #region private functions

        private void ClosingForm()
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private NhanVien MaptoObject()
        {
            NhanVien nv = new NhanVien();
            nv.Ho = this.txtHo.Text.Trim();
            nv.Ten = this.txtTen.Text.Trim();
            nv.Cmnd = this.txtCMND.Text.Trim();
            nv.DiaChi = this.txtDiaChi.Text.Trim();
            nv.DienThoai = this.txtDienThoai.Text.Trim();
            nv.GioiTinh = this.cmbGioiTinh.SelectedItem.ToString();
            nv.LoaiNhanVien = this.cmbLoaiNhanVien.SelectedItem.ToString();
            nv.NgayVaoLam = dtNgayVaoLam.Value;
            nv.MaNhanVien = EmployeeID;
            return nv;
        }

        private void MaptoUI(NhanVien nv)
        {
            txtHo.Text = nv.Ho;
            txtTen.Text = nv.Ten;
            txtCMND.Text = nv.Cmnd;
            txtDiaChi.Text = nv.DiaChi;
            txtDienThoai.Text = nv.DienThoai;
            dtNgayVaoLam.Value =  nv.NgayVaoLam.Value;
            cmbGioiTinh.SelectedItem = nv.GioiTinh;
            cmbLoaiNhanVien.SelectedItem = nv.LoaiNhanVien;

        }
        private void RefreshScreen()
        {
            txtHo.Text = "";
            txtTen.Text ="";
            txtCMND.Text = "";
            txtDiaChi.Text ="";
            txtDienThoai.Text = "";
            //dtNgayVaoLam.Value= DateTime.;
            cmbGioiTinh.SelectedItem = "";
            cmbLoaiNhanVien.SelectedItem ="";

        }
        #endregion

       

    }
}
